package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CategoryAdapter;
import flc.ast.databinding.ActivityCategoryBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

public class CategoryActivity extends BaseAc<ActivityCategoryBinding> {

    private List<StkTagResBean> listMovie = new ArrayList<>();
    private List<StkTagResBean> listPalylet = new ArrayList<>();
    private CategoryAdapter categoryAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int type = 0;
    private int selPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_category;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivCategoryBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvCategoryMovie.setOnClickListener(this);
        mDataBinding.tvCategoryPlaylet.setOnClickListener(this);
        mDataBinding.tvCategory1.setOnClickListener(this);
        mDataBinding.tvCategory2.setOnClickListener(this);
        mDataBinding.tvCategory3.setOnClickListener(this);
        mDataBinding.tvCategory4.setOnClickListener(this);


        mDataBinding.rvCategoryList.setLayoutManager(new GridLayoutManager(mContext, 2));
        categoryAdapter = new CategoryAdapter();
        mDataBinding.rvCategoryList.setAdapter(categoryAdapter);
        categoryAdapter.setOnItemClickListener(this);

        mDataBinding.refreshCategoryItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshCategoryItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshCategoryItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                goToGetData();
                mDataBinding.refreshCategoryItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                goToGetData();
                mDataBinding.refreshCategoryItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCategoryMovie://影视剧
                type = 0;
                clearSelection();
                mDataBinding.tvCategoryMovie.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvCategoryMovie.setTextSize(18);
                goToGetData();
                break;
            case R.id.tvCategoryPlaylet://短剧
                type = 1;
                clearSelection();
                mDataBinding.tvCategoryPlaylet.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvCategoryPlaylet.setTextSize(18);
                goToGetData();
                break;
            case R.id.tvCategory1://悬疑
                selPosition = 0;
                clearCategorySelection();
                mDataBinding.tvCategory1.setBackgroundResource(R.drawable.axuanzhong);
                goToGetData();
                break;
            case R.id.tvCategory2://古典
                selPosition = 1;
                clearCategorySelection();
                mDataBinding.tvCategory2.setBackgroundResource(R.drawable.axuanzhong);
                goToGetData();
                break;
            case R.id.tvCategory3://喜剧
                selPosition = 2;
                clearCategorySelection();
                mDataBinding.tvCategory3.setBackgroundResource(R.drawable.axuanzhong);
                goToGetData();
                break;
            case R.id.tvCategory4://爱情
                selPosition = 3;
                clearCategorySelection();
                mDataBinding.tvCategory4.setBackgroundResource(R.drawable.axuanzhong);
                goToGetData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getMovieData();
        getPlayletData();
    }

    private void goToGetData() {
        if (type == 0) {
            getData(listMovie.get(selPosition).getHashid());
        } else {
            getData(listPalylet.get(selPosition).getHashid());
        }
    }

    private void getMovieData() {
        StkResApi.getChildTagList(this, AppConfig.MOVIE_CATEGORY_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listMovie.addAll(result);
                        goToGetData();
                    }
                }
            }
        });
    }

    private void getPlayletData() {
        StkResApi.getChildTagList(this, AppConfig.PLAYLET_CATEGORY_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listPalylet.addAll(result);
                    }
                }
            }
        });
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + url, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            categoryAdapter.setList(result);
                        } else {
                            categoryAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvCategoryMovie.setTextColor(Color.parseColor("#979590"));
        mDataBinding.tvCategoryMovie.setTextSize(15);
        mDataBinding.tvCategoryPlaylet.setTextColor(Color.parseColor("#979590"));
        mDataBinding.tvCategoryPlaylet.setTextSize(15);
    }

    private void clearCategorySelection() {
        page = 1;
        mDataBinding.tvCategory1.setBackgroundResource(0);
        mDataBinding.tvCategory2.setBackgroundResource(0);
        mDataBinding.tvCategory3.setBackgroundResource(0);
        mDataBinding.tvCategory4.setBackgroundResource(0);
    }

}